package com.foreknow.elm.service.impl;


import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<DeliveryAddress> selectByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            list = deliveryAddressDao.selectByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return list;
    }

    @Override
    public DeliveryAddress selectById(Integer daId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        try {
            deliveryAddress = deliveryAddressDao.selectById(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return deliveryAddress;
    }

    @Override
    public Integer saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int result = 0;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {

            DBUtil.beginTransaction();//提交事务
            result = deliveryAddressDao.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
            DBUtil.commitTransaction();  //提交一个事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }

    @Override
    public Integer updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId) {
        int result = 0;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.beginTransaction();//提交事务
            result = deliveryAddressDao.updateDeliveryAddress(daId, contactName, contactSex, contactTel, address, userId);
            DBUtil.commitTransaction();  //提交一个事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }

    @Override
    public Integer removeDeliveryAddress(Integer daId) {
        int result = 0;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.beginTransaction();//提交事务
            result = deliveryAddressDao.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();  //提交一个事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();//回滚一个事务
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return result;
    }
}
